package prefuse.tp.svn;

import java.awt.Color;
import java.awt.Font;
import java.util.Iterator;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import prefuse.Visualization;
import prefuse.data.Table;
import prefuse.data.Tuple;
import prefuse.data.tuple.TupleSet;

public class GraphRadial_jTable extends JTable {
	
	private static final long serialVersionUID = -4606778854074167520L;

	public GraphRadial_jTable(DefaultTableModel dtm){
		super(dtm);
		setOpaque(true);
		setFont(new Font("Tahoma", Font.PLAIN, 10));
		setGridColor(Color.GRAY);
		setBorder(null);
		setSelectionMode(0);
	}
	
	@SuppressWarnings("unchecked")
	public void jTable_Changed(TableModelEvent tme, DefaultTableModel dtm, 
								TupleSet tupleSelected, Visualization vis,int type){
		int index = 0;
		Iterator it = tupleSelected.tuples();     			
		while( it.hasNext() ) { 
			Tuple t = (Tuple)it.next();
			if (type == 0) index = tme.getLastRow();
			if (type == 1) index = tme.getLastRow()+123;
			if (type == 2) index = tme.getLastRow()+154;
			if ( t.getRow() == index) {
				t.set(dtm.getColumnName(tme.getColumn()),((TableModel)tme.getSource()).getValueAt(tme.getLastRow(),tme.getColumn()));
				vis.run("filter");
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public void jTable_valueChanged(ListSelectionEvent lse, Table table,
			TupleSet tupleSelected, Visualization vis,int type) {
		int index = 0;
		if (type == 1) index = 123;
		if (type == 2) index = 154;
			Iterator it = tupleSelected.tuples(); 
			while( it.hasNext() ) {  
				Tuple t = (Tuple)it.next(); 
				if ( t.getString("Code") == table.get((((ListSelectionModel)lse.getSource()).getAnchorSelectionIndex()) + index,0) ) { 
				vis.getGroup(Visualization.FOCUS_ITEMS).clear();
				vis.getGroup(Visualization.FOCUS_ITEMS).setTuple(t);
				vis.repaint();
				vis.run("filter");
			}
		}
	}
	
}